//
//  Hello World server
//  Connects REP socket to tcp://*:5560
//  Expects "Hello" from client, replies with "World"
//

package main

import (
	"fmt"
	"os"
	"zmq"
)

func zmq_main() os.Error {
	context, err := zmq.Init(1)
	if err != nil {
		return err
	}
	defer context.Close()

	responder, err := context.Socket(zmq.REP)
	if err != nil {
		return err
	}
	defer context.Close()

	err = responder.Connect("tcp://localhost:5560")
	if err != nil {
		return err
	}

	for {
		data, err := responder.Recv(0)
		if err != nil {
			return err
		}
		fmt.Printf("Received request: %s\n", data)
		zmq.Sleep(1)
		err = responder.Send([]byte("World"), 0)
		if err != nil {
			return err
		}
	}
	return nil
}

func main() {
	fmt.Println("Hello World server")
	err := zmq_main()
	if err != nil {
		fmt.Println(err)
	}
}
